package vn.candyapp.qlct.fragment;

import vn.candyapp.qlct.AddTransactionActivity;
import vn.candyapp.qlct.DSGiaoDichActivity;
import vn.candyapp.qlct.GioiThieuActivity;	// no vua them do
import vn.candyapp.qlct.R;
import vn.candyapp.qlct.adapter.AccountAdapter;
import vn.candyapp.qlct.item.KieuTKGiaoDich;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


public class TabThuChi extends BaseFragment implements View.OnClickListener {
	
	private Button btnAdd, btnToday, btnThisWeek, btnThisMonth, btnThisYear, btnIntroduce;
	private ListView accountListView;
	private AccountAdapter accountAdapter;
	private TextView soduTextView;
	
	public static TabThuChi newInstance() {
		TabThuChi f = new TabThuChi();
		return f;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.tab_thuchi, container, false);
		initWidgets(v);
		return v;
	}
	
	public void initWidgets(View v) {
		// button them moi giao dich
		btnAdd = (Button) v.findViewById(R.id.btnAddTransaction);
		btnAdd.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(getActivity(), AddTransactionActivity.class), 1);
			}
		});
//		btnAdd.setOnClickListener(this);
		btnToday = (Button) v.findViewById(R.id.btnShowToday);
		btnToday.setOnClickListener(this);
		btnThisWeek = (Button) v.findViewById(R.id.btnShowThisWeek);
		btnThisWeek.setOnClickListener(this);
		btnThisMonth = (Button) v.findViewById(R.id.btnShowThisMonth);
		btnThisMonth.setOnClickListener(this);
		btnThisYear = (Button) v.findViewById(R.id.btnShowThisYear);
		btnThisYear.setOnClickListener(this);
		btnIntroduce = (Button) v.findViewById(R.id.btnGioiThieu);
		btnIntroduce.setOnClickListener(this);
		soduTextView = (TextView) v.findViewById(R.id.availableBalance);
		accountListView = (ListView) v.findViewById(R.id.lvAccount);
		accountAdapter = new AccountAdapter(getActivity());
		accountAdapter.loadData(new Runnable() {
			
			@Override
			public void run() {
				accountListView.setAdapter(accountAdapter);
				soduTextView.setText(accountAdapter.getTotalSum());
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.btnAddTransaction:
				startActivityForResult(new Intent(getActivity(), AddTransactionActivity.class), 1);
				break;
			case R.id.btnShowToday:
				Intent intent = new Intent(getActivity(), DSGiaoDichActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("TYPE", KieuTKGiaoDich.HOMNAY);
				intent.putExtra("DATA", bundle);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(intent, 1);
				break;
			case R.id.btnShowThisWeek:
				intent = new Intent(getActivity(), DSGiaoDichActivity.class);
				bundle = new Bundle();
				bundle.putSerializable("TYPE", KieuTKGiaoDich.TUANNAY);
				intent.putExtra("DATA", bundle);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(intent, 1);
				break;
			case R.id.btnShowThisMonth:
				intent = new Intent(getActivity(), DSGiaoDichActivity.class);
				bundle = new Bundle();
				bundle.putSerializable("TYPE", KieuTKGiaoDich.THANGNAY);
				intent.putExtra("DATA", bundle);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(intent, 1);
				break;
			case R.id.btnShowThisYear:
				intent = new Intent(getActivity(), DSGiaoDichActivity.class);
				bundle = new Bundle();
				bundle.putSerializable("TYPE", KieuTKGiaoDich.NAMNAY);
				intent.putExtra("DATA", bundle);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(intent, 1);
				break;
			case R.id.btnGioiThieu:
				intent = new Intent(getActivity(),GioiThieuActivity.class);
				startActivity(intent);
				break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
			refreshData();
		}
	}
	
	private void refreshData() {
		accountAdapter.loadData(new Runnable() {
			
			@Override
			public void run() {
				accountAdapter.notifyDataSetChanged();
				soduTextView.setText(accountAdapter.getTotalSum());
			}
		});
	}
	
}
